package com.enterprise.support.utility.generator.pub;

import com.enterprise.support.utility.generator.CodeGenerator;
import com.jgoodies.looks.Options;
import com.jgoodies.looks.plastic.PlasticLookAndFeel;
import com.jgoodies.looks.windows.WindowsLookAndFeel;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;

/**
 * Builds the menu bar.
 *
 * @author ChunBiao Guo
 */
public class MenuBarView {

    private CodeGenerator codeGenerator = null;

    private static MenuBarView instance;

    public MenuBarView(CodeGenerator frame) {
        codeGenerator = frame;
        instance = this;
    }

    public static MenuBarView getDefault() {
        return instance;
    }

    /**
     * Builds, configures, and returns the menubar.
     */
    public JMenuBar buildMenuBar(Settings settings, ActionListener helpActionListener, ActionListener aboutActionListener) {
        JMenuBar bar = new JMenuBar();
        bar.putClientProperty(Options.HEADER_STYLE_KEY, settings.getMenuBarHeaderStyle());
        bar.putClientProperty(PlasticLookAndFeel.BORDER_STYLE_KEY, settings.getMenuBarPlasticBorderStyle());
        bar.putClientProperty(WindowsLookAndFeel.BORDER_STYLE_KEY, settings.getMenuBarWindowsBorderStyle());
        bar.putClientProperty(PlasticLookAndFeel.IS_3D_KEY, settings.getMenuBar3DHint());
        bar.add(buildSystemMenu());
        //bar.add(buildSettingsMenu());
        //bar.add(buildHelpMenu());
        return bar;
    }

    /**
     * Builds and returns the file menu.
     */
    private JMenu buildSystemMenu() {
        JMenu menu = createMenu("系统");
        JMenuItem item = createMenuItem("退出");
        item.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        //menu.addSeparator();
        menu.add(item);
        return menu;
    }

    private JMenu buildSettingsMenu() {
        JMenu menu = createMenu("设置");
        JMenuItem item = createMenuItem("Settings");
        item.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //new SettingsPanel();
            }
        });
        //menu.addSeparator();
        menu.add(item);
        return menu;
    }

    private Component buildHelpMenu() {
        JMenuItem item;
        JMenu menu = createMenu("帮助");
        return menu;
    }

    // Factory Methods ********************************************************

    protected JMenu createMenu(String text) {
        JMenu menu = new JMenu(text);
        return menu;
    }

    protected JMenu createMenu(String text, char mnemonic) {
        JMenu menu = new JMenu(text);
        menu.setMnemonic(mnemonic);
        return menu;
    }

    protected JMenuItem createMenuItem(String text) {
        return new JMenuItem(text);
    }

    protected JMenuItem createMenuItem(String text, char mnemonic) {
        return new JMenuItem(text, mnemonic);
    }

    protected JMenuItem createMenuItem(String text, char mnemonic, KeyStroke key) {
        JMenuItem menuItem = new JMenuItem(text, mnemonic);
        menuItem.setAccelerator(key);
        return menuItem;
    }

    protected JMenuItem createMenuItem(String text, Icon icon) {
        return new JMenuItem(text, icon);
    }

    protected JMenuItem createMenuItem(String text, Icon icon, char mnemonic) {
        JMenuItem menuItem = new JMenuItem(text, icon);
        menuItem.setMnemonic(mnemonic);
        return menuItem;
    }

    protected JMenuItem createMenuItem(String text, Icon icon, char mnemonic,
                                       KeyStroke key) {
        JMenuItem menuItem = createMenuItem(text, icon, mnemonic);
        menuItem.setAccelerator(key);
        return menuItem;
    }

    protected JRadioButtonMenuItem createRadioButtonMenuItem(String text,
                                                             boolean selected) {
        return new JRadioButtonMenuItem(text, selected);
    }

    protected JCheckBoxMenuItem createCheckBoxMenuItem(String text,
                                                       boolean selected) {
        return new JCheckBoxMenuItem(text, selected);
    }

    // Subclass will override the following methods ***************************

    /**
     * Checks and answers whether the quit action has been moved to an operating
     * system specific menu, e.g. the OS X application menu.
     *
     * @return true if the quit action is in an OS-specific menu
     */
    protected boolean isQuitInOSMenu() {
        return false;
    }

    /**
     * Checks and answers whether the about action has been moved to an
     * operating system specific menu, e.g. the OS X application menu.
     *
     * @return true if the about action is in an OS-specific menu
     */
    protected boolean isAboutInOSMenu() {
        return false;
    }

    // Higher Level Factory Methods *****************************************

    /**
     * Creates and returns a JRadioButtonMenuItem with the given enablement and
     * selection state.
     */
    private JRadioButtonMenuItem createRadioItem(boolean enabled,
                                                 boolean selected) {
        JRadioButtonMenuItem item = createRadioButtonMenuItem(getToggleLabel(
                enabled, selected), selected);
        item.setEnabled(enabled);
        item.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                JRadioButtonMenuItem source = (JRadioButtonMenuItem) e
                        .getSource();
                source.setText(getToggleLabel(source.isEnabled(), source
                        .isSelected()));
            }
        });
        return item;
    }

    /**
     * Creates and returns a JCheckBoxMenuItem with the given enablement and
     * selection state.
     */
    private JCheckBoxMenuItem createCheckItem(boolean enabled, boolean selected) {
        JCheckBoxMenuItem item = createCheckBoxMenuItem(getToggleLabel(enabled,
                selected), selected);
        item.setEnabled(enabled);
        item.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                JCheckBoxMenuItem source = (JCheckBoxMenuItem) e.getSource();
                source.setText(getToggleLabel(source.isEnabled(), source
                        .isSelected()));
            }
        });
        return item;
    }

    /**
     * Returns an appropriate label for the given enablement and selection
     * state.
     */
    protected String getToggleLabel(boolean enabled, boolean selected) {
        String prefix = enabled ? "Enabled" : "Disabled";
        String suffix = selected ? "Selected" : "Deselected";
        return prefix + " and " + suffix;
    }

    // Helper Code ************************************************************

    /**
     * Looks up and returns an icon for the specified filename suffix.
     */
    private ImageIcon readImageIcon(String filename) {
        URL url = getClass().getClassLoader().getResource("images/" + filename);
        return new ImageIcon(url);
    }

    private ImageIcon readMenuIcon(String filename) {
        URL url = getClass().getClassLoader().getResource(
                "menu-images/" + filename);
        return new ImageIcon(url);
    }

    /**
     * Creates and returns a submenu labeled with the given text.
     */
    private JMenu createSubmenu(String text) {
        JMenu submenu = new JMenu(text);
        submenu.add(new JMenuItem("Item 1"));
        submenu.add(new JMenuItem("Item 2"));
        submenu.add(new JMenuItem("Item 3"));
        return submenu;
    }

}
